package com.coderman.infosys.auth.app.support;


import com.alibaba.fastjson.JSON;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author 2022-01-02
 * @since 2020/1/8 3:58 下午
 */
@Component
@Aspect
@Slf4j
public class FacadeAdvisor {
    @Around(value = "execution( public  com.coderman.infosys.plat.auth.common.dto.ResultDataDto com.coderman.infosys.auth.app.facadeimpl..*(..))")
    public Object transferException(ProceedingJoinPoint joinPoint) throws Exception {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        try {
            log.info("{}.{} param:{}", method.getDeclaringClass().getName(), method.getName(), JSON.toJSONString(joinPoint.getArgs()));
            Object result = joinPoint.proceed();
            log.info("{}.{} result:{}", method.getDeclaringClass().getName(), method.getName(), JSON.toJSONString(result));
            return result;
        } catch (Throwable exception) {
            log.warn("接口{}.{} throw exception", method.getDeclaringClass().getName(), method.getName(), exception);
            String msg = "未知错误";
            if (StringUtils.isNotBlank(exception.getMessage())) {
                msg = exception.getMessage();
            }
            return ResultDataDto.fail("500",msg);
        }
    }
}
